/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 23
 * Time: 21:06
 */
//牛客 链表相加(二)
//法一 我的方法
class Solution6 {
    public ListNode addInList (ListNode head1, ListNode head2) {
        if(head1 == null) return head2;
        if(head2 == null) return head1;
        ListNode pHead1 = ReverseList (head1);
        ListNode pHead2 = ReverseList (head2);
        int carry = 0;
        ListNode cur = new ListNode((pHead1.val + pHead2.val + carry) % 10);
        ListNode head = cur;
        carry = (pHead1.val + pHead2.val + carry) / 10;
        if(pHead1 != null) {
            pHead1 = pHead1.next;
        }
        if(pHead2 != null) {
            pHead2 = pHead2.next;
        }
        while(pHead1 != null || pHead2 != null) {
            if(pHead1 != null && pHead2 != null) {
                cur.next = new ListNode((pHead1.val + pHead2.val + carry) % 10);
                carry = (pHead1.val + pHead2.val + carry) / 10;
                cur = cur.next;
                if(pHead1 != null) {
                    pHead1 = pHead1.next;
                }
                if(pHead2 != null) {
                    pHead2 = pHead2.next;
                }
            }else if(pHead1 == null && pHead2 != null) {
                cur.next = new ListNode((pHead2.val + carry) % 10);
                carry = (pHead2.val + carry) / 10;
                cur = cur.next;
                if(pHead2 != null) {
                    pHead2 = pHead2.next;
                }
            } else if (pHead1 != null && pHead2 == null) {
                cur.next = new ListNode((pHead1.val + carry) % 10);
                carry = (pHead1.val + carry) / 10;
                cur = cur.next;
                if(pHead1 != null) {
                    pHead1 = pHead1.next;
                }
            }
        }
        if(carry != 0) {
            cur.next = new ListNode(carry);
        }
        return ReverseList (head);
    }
    public ListNode ReverseList (ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }
}


//法二 其它方法
class Solution7 {
    public ListNode addInList (ListNode head1, ListNode head2) {
        if(head1 == null) return head2;
        if(head2 == null) return head1;
        ListNode pHead1 = ReverseList (head1);
        ListNode pHead2 = ReverseList (head2);
        int carry = 0;
        ListNode head = new ListNode(-1);
        ListNode newHead = head;
        while(pHead1 != null || pHead2 != null) {
            int sum = carry;
            if(pHead1 != null) {
                sum += pHead1.val;
                pHead1 = pHead1.next;
            }
            if(pHead2 != null) {
                sum += pHead2.val;
                pHead2 = pHead2.next;
            }
            newHead.next = new ListNode(sum % 10);
            newHead = newHead.next;
            carry = sum / 10;
        }
        if(carry != 0) {
            newHead.next = new ListNode(carry);
        }
        return ReverseList (head.next);
    }
    public ListNode ReverseList (ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }
}